package com.dx.mes.service.uas.service;

import com.dx.common.table.ppt.Bis_uas_fnc_grp;
import com.dx.common.table.ppt.Bis_uas_grp;
import com.dx.common.table.ppt.Bis_uas_usr_grp;
import com.dx.common.table.ppt.Bis_user;
import com.dx.mes.common.dao.IBisUasFncGrpDao;
import com.dx.mes.common.dao.IBisUasGrpDao;
import com.dx.mes.common.dao.IBisUserDao;
import com.dx.mes.facade.uas.service.IBisUasFunGrpService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service("bisUasFunGrpService")
public class BisUasFunGrpService implements IBisUasFunGrpService {

    @Autowired
    private IBisUserDao bisUserDao;
    @Autowired
    private IBisUasFncGrpDao bisUasFncGrpDao;
    @Autowired
    private IBisUasGrpDao bisUasGrpDao;

    @Override
    public Bis_user getUserByUsrID(String userId) {
        Bis_user bis_user = bisUserDao.get(userId);
        return bis_user;
    }

    @Override
    public List<Bis_uas_fnc_grp> getPermission(String userId) {
        String sql = "SELECT * FROM bis_uas_fnc_grp WHERE group_id_fk IN (SELECT group_id_fk FROM bis_uas_usr_grp WHERE usr_id_fk = '"+userId+"')";
        List<Bis_uas_fnc_grp> list = bisUasFncGrpDao.findBySQL(sql, Bis_uas_fnc_grp.class);
        return list;
    }

    @Override
    public List<Bis_uas_grp> getAdminFlag(String userId) {
        String sql = "SELECT * FROM bis_uas_grp WHERE group_id IN (SELECT group_id_fk FROM bis_uas_usr_grp WHERE usr_id_fk = '"+userId+"') AND admin_flg = 'Y'";
        List<Bis_uas_grp> list = bisUasGrpDao.findBySQL(sql, Bis_uas_grp.class);
        return list;
    }

    @Override
    public void editPwdById(String userid, String newPwd) {
        Bis_user bis_user = bisUserDao.get(userid);
        bis_user.setUsr_key(newPwd);
        bis_user.setEvt_timestamp(new Timestamp(new Date().getTime()));
        bis_user.setReg_timestamp(new Timestamp(new Date().getTime()));
        bisUserDao.update(bis_user);
    }

    //根据用户名和密码查找用户
    public boolean getUserByUsrIdAndPwd(String usrId, String pwd) {
        boolean flag = true;
        Bis_user bis_user = bisUserDao.get(usrId);
        if (bis_user == null) {
            flag = false;
        } else {
            if (StringUtils.isNotBlank(bis_user.getUsr_key())) {
                if (!pwd.equals(bis_user.getUsr_key()) || "BC".equals(bis_user.getSystem_id()) || "".equals(bis_user.getSystem_id())) {//不是bc的用户
                    flag = false;
                }
            } else {
                flag = false;
            }
            if (!"Y".equals(bis_user.getValid_flg())) {
                flag = false;
            }
        }
        return flag;
    }
}
